package com.flow.tools;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;

import com.flow.model.PagerModel;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class PagerInterceptor implements Interceptor {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1275525226072650860L;
	

	public void destroy() {

	}

	public void init() {

	}

	public String intercept(ActionInvocation invocation) throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		PagerModel.setOffset(getOffset(request));
		PagerModel.setPagesize(getPagesize(request));
		try {
			return invocation.invoke();
		} finally {
			PagerModel.removeOffset();
			PagerModel.removePagesize();
		}
	}

	protected int getOffset(HttpServletRequest request) {
		if (request.getParameter("pager.offset") == null) {
			return 0;
		} else {
			return Integer.parseInt(request.getParameter("pager.offset"));
		}
	}

	protected int getPagesize(HttpServletRequest request) {
		if (request.getParameter("pagesize") == null) {
			return 10;
		} else {
			return Integer.parseInt(request.getParameter("pagesize"));
		}
	}


}
